package com.junsi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.ApproveRole;
import com.junsi.bean.Rank;
import com.junsi.service.RankService;

import net.sf.json.JSONArray;
/**
 * 客户级别控制器
 * @author lenovo
 *
 */
@Controller
@RequestMapping("/rank")
public class RankController {
  
	@Autowired
	private RankService rankService;
	Logger logger=Logger.getRootLogger();

	/**
	 * 新增
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public ModelAndView insert(Rank rank, HttpServletResponse response) throws Exception {
		   rankService.insert(rank);
		   ModelAndView modelAndView=new ModelAndView();
			  modelAndView.setViewName("redirect:/rank/select.action");
		      return modelAndView;
	}
	/**
	 * 查询
	 */
	@RequestMapping("/findflowNme")
	@ResponseBody
	public void findflowNme( HttpServletResponse response){
		  List<Rank> rank=rankService.listAll();
		  List<Map<String, String>> array = new ArrayList<Map<String, String>>();
			for (Rank arrayList : rank) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("approveRoleId", arrayList.getRankId().toString());
				map.put("flowNme", arrayList.getRankName());
				array.add(map);
			}
			JSONArray jsonArray = JSONArray.fromObject(array);
			response.setContentType("text/html; charset=UTF-8");
			try {
				response.getWriter().write(jsonArray.toString());
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	/**
	 * 刪除
	 */
	@RequestMapping("/deletes")
	@ResponseBody
	public ModelAndView deletes(Integer id, HttpServletResponse response) {
		 rankService.delete(id);
		ModelAndView modelAndView=new ModelAndView();
		  modelAndView.setViewName("redirect:/rank/select.action");
	      return modelAndView;
		
	}

	/**
	 * 更新
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/updata")
	@ResponseBody
	public ModelAndView updata(Rank rank, HttpServletResponse response) throws Exception {
		rankService.update(rank);
		ModelAndView modelAndView=new ModelAndView();
		  modelAndView.setViewName("redirect:/rank/select.action");
	      return modelAndView;
	}

	/**
	 * 查詢
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/select")
	@ResponseBody
	public ModelAndView select(HttpServletResponse response) throws Exception {
		ModelAndView modelAndView=new ModelAndView();
		List<Rank> list = rankService.listAll();
	      modelAndView.addObject("list", list);
	      modelAndView.setViewName("forward:/page/kehu.jsp");
	      return modelAndView;
	}
}
